/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scms;

import java.io.*;

/**
 * can read string and ints from the screen
 * @author pumped
 */
public class StreamIO {

	/**
	 * reads a string from user input
	 * @param message  Prompt to give user.
	 * @return  Returns string user entered.
	 */
	public static String readString(String message) {
		InputStreamReader sr = new InputStreamReader(System.in);
		BufferedReader read = new BufferedReader(sr);
		String input;
		System.out.print(message);
		try {
			input = read.readLine();
			return input;
		} catch (IOException ioe) {
			System.out.println("Error - Invalid input");
		}

		return null;
	}

	/**
	 * reads int from user input
	 * @param message  Prompt to give user.
	 * @return  int user entered
	 */
	public static int readInt(String message) {
		int num = 0;
		String input = "";

		while (input.equals("")) {
			input = readString(message);
			try {
				num = Integer.parseInt(input);
			} catch (NumberFormatException nfe) {
				System.out.println("Error - Invalid number");
				input = "";
			}
		}


		return num;
	}

	/**
	 * reads int from user input, within bounds
	 * @param message  Prompt to give user.
	 * @param lower  Lower bound of input
	 * @param upper  Upper bound of input
	 * @return  int user entered
	 */
	public static int readInt(String message, int lower, int upper) {
		int num = 0;
		num = readInt(message);
		while (num < lower || num > upper) {
			System.out.println("Invalid selection!");
			num = readInt(message);
		}
		return num;
	}
}
